package by.jabbby.ui.act;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import by.jabbby.Logger;
import by.jabbby.service.JabberServiceProxy;
import by.jabbby.ui.data.RosterEntryObj;

public class Roster extends TitleBar {
	private JabberServiceProxy jabberProxy;
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			
			jabberProxy = JabberServiceProxy.getInstance(this);	
			setContentView(R.layout.rosters);
			super.initTitleBar();
			
		} catch (Exception e) {
			Logger.e(e);
		}

	}
	
	protected void initRosterView(){
		try {
			RosterEntryObj[] rosters =  jabberProxy.getService().getRosters();
			if(rosters!=null){
				TableLayout contactTL = getContactsTable();
				int i = 0;				
				for(RosterEntryObj r :rosters){
					TableRow tr = new TableRow(this);
					TextView tv = new TextView(this);
					final String user = r.getUser();
					tv.setText(user);
					
					Button chatBtn = new Button(this);
					chatBtn.setText(R.string.chat_btn);
					chatBtn.setOnClickListener(new View.OnClickListener() {						
						public void onClick(View v) {							
							Intent intent = new Intent( getApplicationContext(), Chat.class);
							intent.putExtra("by.jabbby.ui.act.user_jid", user);
							Roster.this.startActivity(intent);
							
						}
					});
					
					Button editBtn = new Button(this);
					editBtn.setText(R.string.edit_btn);
					
					tr.addView(tv, 0);
					tr.addView(chatBtn, 1);
					tr.addView(editBtn, 2);
					contactTL.addView(tr );
					i++;					
				}
			}		
		} catch (Exception e) {
			Logger.e(e);
		} 
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		// TODO Auto-generated method stub
		return super.onMenuOpened(featureId, menu);
	}

/*	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		MenuInflater inflater = getMenuInflater();

		inflater.inflate(R.menu.primary_menu23, menu);

		return true;
	}
*/
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
	//	menuUtil.onMenuItemSelected(featureId, item);

		/*
		 * AccountProvider ap = new
		 * AccountProvider(this.getApplicationContext()); Account acc =
		 * ap.getCurrentAccount();
		 * 
		 * if( item.getItemId() == R.id.account){ Intent settingsActivity = new
		 * Intent(getBaseContext(), AccauntPreference.class);
		 * startActivity(settingsActivity); }
		 */
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onStart(){
		Logger.e("Roster.onStart()");
		super.onStart();
		this.getLeftTextView().setText(R.string.title_bar_contacts);
		if(jabberProxy.isServiceBinded()){
			//jabberProxy.getService().activeChat(this.out);
			initRosterView();
		}
	}

	@Override
	protected void onDestroy() {
		Logger.e("Roster.onDestroy()");
	//	unRegisterRecivers();
		super.onDestroy();

	}

	@Override
	protected void onPause() {
		Logger.e("Roster.onPause()");
	//	unRegisterRecivers();
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Logger.e("Roster.onRestart()");
		TableLayout contactTL = getContactsTable();
		contactTL.removeAllViews();
		
	//	unRegisterRecivers();
	//	registerRecivers();
		super.onRestart();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		Logger.e("Roster.onRestoreInstanceState()");
	//	registerRecivers();
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		Logger.e("Roster.onResume()");
	//	registerRecivers();
		super.onResume();
	}

	public void onJabberServiceBinded() {
		super.onJabberServiceBinded();
		initRosterView();
		
	}
	
	private TableLayout getContactsTable(){
		TableLayout contactTL = (TableLayout) this.findViewById(R.id.ContactTableLayout);
		return contactTL;
	}

}
